package com.yuanluludev.permission

import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.tbruyelle.rxpermissions3.RxPermissions
import com.yuanluludev.lib_utils.util.ActivityUtils
import com.yuanluludev.lib_utils.util.AppUtils

object RxPermissionUtils {

    private const val ACCESS_BACKGROUND_LOCATION = "android.permission.ACCESS_BACKGROUND_LOCATION"

    /**
     * 在Activity里面申请权限
     */
    fun requestPermissionInActivity(
        activity: FragmentActivity,
        vararg permissions: String,
        onGranted: (() -> Unit)? = null,
        isShowGuideDialog: Boolean = true
    ) = requestPermission(
        activity = activity,
        permissions = permissions,
        onGranted = onGranted,
        isShowGuideDialog = isShowGuideDialog
    )

    /**
     * 在Fragment里面申请权限
     */
    fun requestPermissionInFragment(
        fragment: Fragment,
        vararg permissions: String,
        onGranted: (() -> Unit)? = null,
        isShowGuideDialog: Boolean = true
    ) = requestPermission(
        fragment = fragment,
        permissions = permissions,
        onGranted = onGranted,
        isShowGuideDialog = isShowGuideDialog
    )

    /**
     * 请求权限
     */
    private fun requestPermission(
        activity: FragmentActivity? = null,
        fragment: Fragment? = null,
        vararg permissions: String,
        onGranted: (() -> Unit)? = null,
        isShowGuideDialog: Boolean
    ) {
        val rxPermissions =
            if (activity != null) RxPermissions(activity) else RxPermissions(fragment!!)
        rxPermissions.requestEachCombined(*permissions)
            .subscribe { permission ->
                when {
                    permission.granted -> {
                        onGranted?.invoke()
                    }
                    permission.shouldShowRequestPermissionRationale -> {
                        //后台定位权限被拒绝了 不在显示 引导弹窗
                        if (permission.name != ACCESS_BACKGROUND_LOCATION) {
                            if (isShowGuideDialog) showOpenSettingsDialog()
                        }
                    }
                    else -> {
                        //后台定位权限被拒绝了 不在显示 引导弹窗
                        if (permission.name != ACCESS_BACKGROUND_LOCATION) {
                            if (isShowGuideDialog) showOpenSettingsDialog()
                        }
                    }
                }
            }
    }

    /**
     * 拒绝权限显示的引导弹窗
     */
    private fun showOpenSettingsDialog() {
        AlertDialog.Builder(ActivityUtils.getTopActivity())
            .setTitle("提示")
            .setMessage("由于安卓系统限制，请您手动开启本软件需要的所有权限，从而保障此软件能正常运行。")
            .setCancelable(false)
            .setNegativeButton(
                "下次再说"
            ) { _, _ ->

            }
            .setPositiveButton(
                "立即设置"
            ) { _, _ ->
                AppUtils.launchAppDetailsSettings()
            }.show()
    }

}